package com.demo.webfluxdemo.handler;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerResponse;

import static org.springframework.web.reactive.function.server.RequestPredicates.*;
import static org.springframework.web.reactive.function.server.RouterFunctions.*;

/**
 * 处理路由
 * @author: lipan
 * @date: 2021/9/24 20:38
 */
@Configuration
public class Routers {


    /**
     * user 模块 方法与 url的路由配置
     * @param userHandler
     * @return
     */
    @Bean
    public RouterFunction<ServerResponse> userRoute(UserHandler userHandler){

        return nest(path("/user"),
                  route(GET("/"),userHandler::getAll)
                   .andRoute(POST("/").and(accept(MediaType.APPLICATION_JSON)),userHandler::createUser)
                   .andRoute(DELETE("/{id}"),userHandler::deleteUserById)
                   .andRoute(PUT("/").and(accept(MediaType.APPLICATION_JSON)),userHandler::update));
    }

}
